<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="姓氏" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="firstName">
              <a-input v-model="model.firstName" placeholder="请输入姓氏"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="lastName">
              <a-input v-model="model.lastName" placeholder="请输入名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="学校" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="school">
              <j-dict-select-tag type="list" v-model="model.school" dictCode="" placeholder="请选择学校" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="email地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="email">
              <a-input v-model="model.email" placeholder="请输入email地址"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="联系电话" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="phone">
              <a-input v-model="model.phone" placeholder="请输入联系电话"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="学校类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="schoolType">
              <j-dict-select-tag type="list" v-model="model.schoolType" dictCode="" placeholder="请选择学校类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="年级" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="grade">
              <j-dict-select-tag type="list" v-model="model.grade" dictCode="" placeholder="请选择年级" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="入学测试成绩" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="testScore">
              <a-input-number v-model="model.testScore" placeholder="请输入入学测试成绩" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="address">
              <a-input v-model="model.address" placeholder="请输入地址"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="房屋类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="houseType">
              <j-dict-select-tag type="list" v-model="model.houseType" dictCode="" placeholder="请选择房屋类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="城市" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="city">
              <a-input v-model="model.city" placeholder="请输入城市"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="地区" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="region">
              <a-input v-model="model.region" placeholder="请输入地区"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="邮政编码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="postalCode">
              <a-input v-model="model.postalCode" placeholder="请输入邮政编码"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="入学年份" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="arrivalYear">
              <a-input v-model="model.arrivalYear" placeholder="请输入入学年份"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="推荐人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="recommendedBy">
              <a-input v-model="model.recommendedBy" placeholder="请输入推荐人"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="父母名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="parentName">
              <a-input v-model="model.parentName" placeholder="请输入父母名称"  ></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'StudentInfoForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           firstName: [
              { required: true, message: '请输入姓氏!'},
           ],
           lastName: [
              { required: true, message: '请输入名称!'},
           ],
           school: [
              { required: true, message: '请输入学校!'},
           ],
           email: [
              { required: true, message: '请输入email地址!'},
              { pattern: /^([\w]+\.*)([\w]+)@[\w]+\.\w{3}(\.\w{2}|)$/, message: '请输入正确的电子邮件!'},
           ],
           phone: [
              { required: true, message: '请输入联系电话!'},
              { pattern: /^-?\d+\.?\d*$/, message: '请输入数字!'},
           ],
           schoolType: [
              { required: true, message: '请输入学校类型!'},
           ],
           grade: [
              { required: true, message: '请输入年级!'},
           ],
           testScore: [
              { required: false},
              { pattern: /^-?\d+\.?\d*$/, message: '请输入数字!'},
           ],
           address: [
              { required: true, message: '请输入地址!'},
           ],
           region: [
              { required: true, message: '请输入地区!'},
           ],
        },
        url: {
          add: "/sistersophie/studentInfo/add",
          edit: "/sistersophie/studentInfo/edit",
          queryById: "/sistersophie/studentInfo/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>